<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\AuthRequest;
use App\Models\User;
use Auth;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * 获取站长信息
     */
    public function webmaster()
    {
        $user = User::with(['skill' => function ($query) {
            $query->where('hidden', '0');
        }, 'experience' => function ($query) {
            $query->where('hidden', '0');
        }])->get();
        return $this->success('获取成功', $user);
    }

    /**
     * 获取用户信息
     * @param Request $request
     * @return mixed
     */
    public function user(Request $request)
    {
        $name = $request->get('name');
        if (!$name) {
            if (Auth::guard('api')->user()) {
                $name = Auth::guard('api')->user()->name;
            } else {
                return $this->error('无用户名称');
            }
        }

        $user = User::with(['skill' => function ($query) {
            $query->where('hidden', '0');
        }, 'experience' => function ($query) {
            $query->where('hidden', '0');
        }])->where('name', $name)->first();

        return $this->success('获取成功', $user);
    }

    /**
     * 更改用户信息
     * @param Request $request
     * name
     * age
     * email
     * sex
     * education
     * grad_school
     * self_intro
     * avatar
     * duration
     * @return
     */
    public function update(Request $request)
    {

        $input = $request->all();
        if (array_key_exists('mobile', $input)) {
            return $this->error('不能修改手机号');
        }

        if (array_key_exists('password', $input)) {
            return $this->error('不能修改密码');
        }

        $user = Auth::guard('api')->user();
        $user->update($request->all());
        
        return $this->success('更新成功', $user);
    }
}
